package e.odbo.data.generate.code;

import e.odbo.data.generate.GenerateConfig;
import e.odbo.data.generate.code.model.DataBaseMeta;

import freemarker.template.Configuration;
import org.jumpmind.db.model.Database;
import org.jumpmind.db.model.Table;

import java.io.*;


public class CodeGenerator {
    GenerateConfig config;

    DataBaseMeta dataBaseMeta;

    Configuration generatorCfg;

    public CodeGenerator(GenerateConfig config) {
        this.config = config;
        generatorCfg = new Configuration(Configuration.VERSION_2_3_27);
    }


    public void generateForModel(Database database, String codeType) {
        dataBaseMeta = new DataBaseMeta();
        dataBaseMeta.setBasePackageName(config.basePackage);
        BeanClass beanGenerate;
        DaoClass daoGenerate;
        String fileName;
        String retTxt;
        File dir = new File(config.code_directory + "/" + config.code_directory_bean);
        dir.deleteOnExit();
        dir.mkdirs();
        System.out.println("grenerateFilePath:"+dir.toString());
        dir = new File(config.code_directory + "/" + config.code_directory_dao);
        dir.deleteOnExit();
        dir.mkdirs();
        System.out.println("greneratePackagePath:"+dir.toString());

        for (Table table : database.getTables()) {
            beanGenerate = new BeanClass(table, config);
            daoGenerate = new DaoClass(beanGenerate, table, config);
            retTxt = beanGenerate.generateClass(generatorCfg, codeType);
            fileName = config.code_directory + "/" + config.code_directory_bean + "/" + beanGenerate.tableMeta.getClassName() + getCodeTypePe(codeType);
            writeToFile(fileName, retTxt);

            retTxt = daoGenerate.generateClass(generatorCfg, codeType);
            fileName = config.code_directory + "/" + config.code_directory_dao + "/" + beanGenerate.tableMeta.getClassName() + "DAO" + getCodeTypePe(codeType);
            writeToFile(fileName, retTxt);
            dataBaseMeta.addTableMeta(beanGenerate.tableMeta);
        }

        DataLogicHelperGenerator helperGenerator = new DataLogicHelperGenerator(dataBaseMeta, config);
        retTxt = helperGenerator.generateClass(generatorCfg, codeType);
        fileName = config.code_directory + "/" + "DataLogicHelper" + getCodeTypePe(codeType);
        writeToFile(fileName, retTxt);
    }


    private void writeToFile(String fileName, String cont) {
        File fileToWrite = new File(fileName);
        fileToWrite.deleteOnExit();
            BufferedWriter writer = null;
            try {
                writer = new BufferedWriter(new FileWriter(fileToWrite));
                writer.write(cont);
                writer.flush();
                System.out.println("writeToFile:"+fileToWrite.toString()+"with :"+cont);
            }catch (Exception e){
                e.printStackTrace();
            }

    }


    private String getCodeTypePe(String codeType) {
        if (codeType.equalsIgnoreCase("javascript"))
            return ".js";
        return ".java";
    }

}
